#!./common/bats/bin/bats
# -*-sh-*-

load ./common/test_helper_functions || exit 1
source ./common/functions || exit 1

if [ -s ./common/TEST_ENV ];then
    source ./common/TEST_ENV
fi

check_rms
rm=$RESOURCE_MANAGER

NODES=2
TASKS=8
ARGS=8

unset OMP_NUM_THREADS


@test "[Extrae] MPI F77 trace under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s mpi_ping ];then
	flunk "mpi_ping binary does not exist"
    fi

    rm -f TRACE.mpits
    rm -f mpi_ping.prv
    rm -rf set-0
    mkdir set-0

    mkdir -p .tmp.extrae
    export MPI2PRV_TMP_DIR=.tmp.extrae

    run_mpi_binary "./tracef.sh ./mpi_ping" "" $NODES $TASKS
    assert_success

    # allow for some job output lag from pbs
    for i in `seq 1 10`; do
	if [ -s TRACE.mpits ];then
	    break
	else
	    sleep 0.2
	fi
    done

    run mpi2prv -f TRACE.mpits 

    run ls mpi_ping.prv
    assert_success

}

@test "[Extrae] Parallel trace merge runs under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {

    run ls TRACE.mpits
    assert_success

    rm -f job.out

    mkdir -p .tmp.extrae
    export MPI2PRV_TMP_DIR=.tmp.extrae

    run_mpi_binary -o "job.out" "mpimpi2prv -syn -f TRACE.mpits -o trace.prv" "" $NODES $TASKS
    assert_success

    run ls trace.prv
    assert_success

    # allow for some job output lag from pbs
    for i in `seq 1 10`; do
	if [ -s job.out ];then
	    break
	else
	    sleep 0.2
	fi
    done

    run ls job.out
    assert_success

    run tail -1 job.out
    assert_output "mpi2prv: Congratulations! trace.prv has been generated."

    rm -rf profile*
    rm -f pingtmp?
    rm -rf *prv *row *pcf TRACE.*
    rm -rf $MPI2PRV_TMP_DIR
#    rm -rf set-0
}
